<?
	$OnPadPage = true;
	
	# Set ID
	$CICUID	= $Page;
	
	# Include pad functions
	include "classes/pad_functions.php"; 
	
	# Get Character from Database;
	$Fields =   array(TBL_CHARACTERS_CICUID,	TBL_CHARACTERS_NAME,	TBL_CHARACTERS_CLEANNAME,	TBL_CHARACTERS_SERVER,
					  TBL_CHARACTERS_URL,		TBL_CHARACTERS_AVATAR,	TBL_CHARACTERS_MUGSHOT,		TBL_CHARACTERS_ADDED,
					  TBL_CHARACTERS_EVENTS,	TBL_CHARACTERS_LASTUPDATED,TBL_CHARACTERS_SCORE,		TBL_CHARACTERS_THEME,
					  TBL_CHARACTERS_BG,		TBL_CHARACTERS_COLOR,	TBL_CHARACTERS_FIXED, 		TBL_CHARACTERS_CHARTCOL,
					  TBL_CHARACTERS_UPDATEWAIT,TBL_CHARACTERS_30HOURPERIOD,TBL_CHARACTERS_AVATAR_HISTORY);
	
	$Character = $DB->GetData(TBL_CHARACTERS, $Fields, false, TBL_CHARACTERS_CICUID ."='". $CICUID ."'");
	//show($Character);
	
	# Was there any errors? If so display
	if (!empty($Character[1][1]))
	{
		$ErrorID 	= $Character[1][1];
		$ErrorMsg	= $Character[1][2];
		
		if ($Session->IsAdmin())
		{
			echo '<div class="notice">
			
				<h2 style="color:#f00;">Fatal Error</h2>
				
				<div class="debugger">
				
				<h3>'. $ErrorID .'</h3>
				<h5>'. $ErrorMsg .'</h5>
				<hr>';
				
			Show($Character);
				
				
			echo'</div></div>';
		}
		else
			echo '<div class="error">'. FATAL_ERROR_CHARACTER .'</div>';
	}
	else
	
	# If character exists
	if ($Character[0])
	{
		# Reset the Character Variable
		$Character 		= $Character[3];
		
		#------------------------------------------------------------------------------------------------------------------
		# Get Config so we know specific privacy settings
		#------------------------------------------------------------------------------------------------------------------
		$CICUID				= $Character[TBL_CHARACTERS_CICUID];
		$Server				= $Character[TBL_CHARACTERS_SERVER];
		
		$Fields 			= array(TBL_CONFIG_VERIFIED, TBL_CONFIG_KEY,TBL_CONFIG_PASSCODE, TBL_CONFIG_TIMLINE, TBL_CONFIG_CLASSES, TBL_CONFIG_PRIVACY);			  	  
		$Config 			= $DB->GetData(TBL_CONFIG, $Fields, false, TBL_CONFIG_CICUID ."='". $CICUID ."'");
		//Show($Config);
		$Config				= $Config[3];
		
		$Passcode 			= $Config[TBL_CONFIG_PASSCODE];
		$SettingsTimeline 	= explode("|", $Config[TBL_CONFIG_TIMLINE]);
		$SettingsClasses 	= explode("|", $Config[TBL_CONFIG_CLASSES]);
		$SettingsPrivacy 	= explode("|", $Config[TBL_CONFIG_PRIVACY]);
		
		//Show($SettingsTimeline);
		
		// Check if private
		if (!empty($Passcode) && $_POST['pc'] != $Passcode)
		{
			echo '<div class="private"><form method="post">
				  <span style="font-size:17px;color:#FFD659;">Password Protected!</span><br><br>
				  Enter Pad Password: <input name="pc" type="password" value="" maxlength="32" style="border:solid 1px #000;" /> <input name="" type="submit" style="border:solid 1px #000;"  /></form></div>';
		}
		else if ($SettingsPrivacy[0] && !$Session->Online)
		{
			echo '<div class="private">
				  <span style="font-size:17px;color:#FFD659;">Sorry!</span><br><br>
				  You must be an XIVPads member to view this characters pad.</div>';
		}
		else if ($SettingsPrivacy[1] && ($Session->GetCharacter('server') != $Server || !$Session->IsAdmin()))
		{
			echo '<div class="private">
				  <span style="font-size:17px;color:#FFD659;">Sorry!</span><br><br>
				  This character pad is private to those on '. $Server .'.</div>';
		}
		else if ($SettingsPrivacy[2] && ($Session->Character != $CICUID || !$Session->IsAdmin()))
		{
			echo '<div class="private">
				  <span style="font-size:17px;color:#FFD659;">Sorry!</span><br><br>
				  This character pad is private.</div>';
		}
		else
		{
		
			# Get Friends from Database
			$Fields =   array(TBL_FRIENDS_ARRAY);	
			$Friends = $DB->GetData(TBL_FRIENDS, $Fields, false, TBL_FRIENDS_CICUID ."='". $CICUID ."'");

			$CharactersFriends = explode(",", $Friends[3][CICUIDArray]);
		
			#------------------------------------------------------------------------------------------------------------------
			# Pull Data
			#------------------------------------------------------------------------------------------------------------------
			
			# Some pre-stuff that may be required:
			$Classes 		= GetClassList();
			$EXPPerLevel 	= GetEXPPerLevel();
	
			# Set some variables		
			$Name 			= $Character[TBL_CHARACTERS_NAME];
			$NameClean		= $Character[TBL_CHARACTERS_CLEANNAME];
			$LODEURL		= $Character[TBL_CHARACTERS_URL];
			$Avatar			= $Character[TBL_CHARACTERS_AVATAR];
			$Mugshot		= $Character[TBL_CHARACTERS_MUGSHOT];
			$AddedRaw		= $Character[TBL_CHARACTERS_ADDED];
			$Added			= TranslateTime($AddedRaw, false);
			$Events			= $Character[TBL_CHARACTERS_EVENTS];
			$LastUdatedRaw	= $Character[TBL_CHARACTERS_LASTUPDATED];
			$LastUdated		= TranslateTime($LastUdatedRaw, false);
			$Score			= $Character[TBL_CHARACTERS_SCORE];
			$ChartColumns	= $Character[TBL_CHARACTERS_CHARTCOL];
			$UpdateWait		= $Character[TBL_CHARACTERS_UPDATEWAIT] * (60 * 60);
			$Update30Hour	= $Character[TBL_CHARACTERS_30HOURPERIOD];
			$AvatarHistory	= $Character[TBL_CHARACTERS_AVATAR_HISTORY];
			
			// Only setup Avatar history if we have some.
			if (!empty($AvatarHistory))
				$AvatarHistory	= explode("|", $Character[TBL_CHARACTERS_AVATAR_HISTORY]);
			
			# Theme settings
			$Theme			= $Character[TBL_CHARACTERS_THEME];
			if (empty($Theme))
				echo '<style> @import "css/light/pad.css"; </style>';
			else
				echo '<style> @import "css/themes/'. $Theme .'"; </style>';
				
			$Background		= $Character[TBL_CHARACTERS_BG];
			$Color			= $Character[TBL_CHARACTERS_COLOR];
			$Fixed			= $Character[TBL_CHARACTERS_FIXED];
			
			# Set fields for stats/prev stats data and pull from Database.
			$Fields = array(TBL_STATS_PROFILE, TBL_STATS_ATTRIBUTES, TBL_STATS_RESISTANCES, TBL_STATS_LEVELS, TBL_STATS_EXP, TBL_STATS_COMPANY);		  
			$Stats = $DB->GetData(TBL_STATS, $Fields, false, TBL_STATS_CICUID ."='". $Character[TBL_CHARACTERS_CICUID] ."'");
			$Stats = $Stats[3];
			$Fields = array(TBL_STATS_AUTO, TBL_STATS_LEVELS, TBL_STATS_EXP);		  
			$PrevStats = $DB->GetData(TBL_STATS_HISTORY, $Fields, true, TBL_STATS_CICUID ."='". $Character[TBL_CHARACTERS_CICUID] ."'", TBL_STATS_AUTO, "DESC", array(0,10));
			//show($PrevStats);
			$HasPrev = $PrevStats[0];
			$PrevStats = $PrevStats[3];
			
			# Set some basic profile data
			$Profile_Data   = explode(":", $Stats[TBL_STATS_PROFILE]);
			$ActiveClass 	= str_replace("&nbsp;", "", $Profile_Data[0]);
			$Race 			= explode("|", $Profile_Data[1]);
			$Flag 			= str_replace(array(" ", "&#39;"), "", $Profile_Data[2]);
			$Guardian		= $Profile_Data[3];
			$Guardian_Icon	= explode(",", strtolower(str_replace("&#39;", "", $Guardian)));
			$Guardian_Icon  = $Guardian_Icon[0];
			$Birthdate		= str_replace("of the", "/", $Profile_Data[4]);
			$Birthdate		= str_replace("of the", "/", $Profile_Data[4]);
			$HP				= explode("-", $Profile_Data[7]);
			$MP				= explode("-", $Profile_Data[8]);
			$TP				= explode("-", $Profile_Data[9]);
			$Levels 		= explode(":", $Stats[TBL_STATS_LEVELS]);
			$Experience		= explode(":", $Stats[TBL_STATS_EXP]);
			$CompanyData	= explode("|", $Stats[TBL_STATS_COMPANY]);
			$GC_Rank		= $CompanyData[1];
			$GC_Icon		= $CompanyData[0];
			//Show($CompanyData);
			
			# Get index of Active Class then set Level and Experience Points.
			$ActiveClass_Index 	= array_search($ActiveClass, $Classes);
			$ActiveClass_Level 	= $Levels[$ActiveClass_Index];
			$ActiveClass_EXP 	= str_replace("|", " / ", $Experience[$ActiveClass_Index]);
			if ($ActiveClass_EXP == "0 / 0")
				$ActiveClass_EXP = "Capped";
			
			# Male/Female detection
			$FemaleDetection = stripos($Race[1], "Female");
			if ($FemaleDetection === false)
				$GenderIcon = "male_icon";
			else
				$GenderIcon = "female_icon";
			
			# Get Related Linkshell Data
			$Fields 	= array(TBL_LINKSHELL_ID, TBL_LINKSHELL_NAME, TBL_LINKSHELL_EMBLUM, TBL_LINKSHELL_OWNER);		
			$Linkshells = $DB->GetData(TBL_LINKSHELL, $Fields, true, TBL_LINKSHELL_CACHE ." LIKE '%". $CICUID ."%'");
			
			# Get the last 5 events
			$Fields = array(TBL_EVENTS_SUBJECT, TBL_EVENTS_DETAILS);
			$RecentEvents = $DB->GetData(TBL_EVENTS, $Fields, true, TBL_EVENTS_CICUID ."='". $CICUID ."'", TBL_EVENTS_AUTO, "DESC", array(0,5));
			$RecentEvents = $RecentEvents[3];
			
			# Get Achievements
			$Fields = array(TBL_HISTORY_CICUID, 
							TBL_HISTORY_NMS, 
							TBL_HISTORY_FACTIONS, 
							TBL_HISTORY_CAMPS,
							TBL_HISTORY_QUESTS,
							TBL_HISTORY_GUILDSKILLS,
							TBL_HISTORY_MISC,
							TBL_HISTORY_GIL,
							TBL_HISTORY_ENEMIES_KILLED,
							TBL_HISTORY_LOCALLEVES_COMPLETE,
							TBL_HISTORY_REGIONLEVES_COMPLETE,
							TBL_HISTORY_TOTAL,
							TBL_HISTORY_COMPANY,
							TBL_HISTORY_DUNGEON,
							TBL_HISTORY_LEVELS,
							TBL_HISTORY_TALLIES,
							TBL_HISTORY_OTHER,
							TBL_HISTORY_PAGES,
							TBL_HISTORY_V3);					
			$Achievements = $DB->GetData(TBL_HISTORY, $Fields, false, TBL_HISTORY_CICUID ." = '". $CICUID ."'");
			$Achievements = $Achievements[3];
			$Pages		  = $Achievements[TBL_HISTORY_PAGES];
			$Version	  = $Achievements[TBL_HISTORY_V3];
			
			# Get SP Gains
			$Fields 	= array(TBL_EVENTS_ID, TBL_EVENTS_TYPE, TBL_EVENTS_SUBJECT, TBL_EVENTS_DETAILS);
			$SPGains 	= $DB->GetData(TBL_EVENTS, $Fields, true, TBL_EVENTS_CICUID ." = '". $CICUID ."' AND ". TBL_EVENTS_TYPE ." = 'misc'", TBL_EVENTS_AUTO, 'DESC');	
			$SPGains_b	= $SPGains[0];
			$SPGains	= $SPGains[3];	
			
			# Get SP GTimeline
			$Fields 	= array(TBL_TIMELINE_LINKS, TBL_TIMELINE_DATES);						
			$SPTimeline	= $DB->GetData(TBL_TIMELINE, $Fields, true, TBL_EVENTS_CICUID ." = '". $CICUID ."'");
			$SPTimeline = $SPTimeline[3];
			
			#------------------------------------------------------------------------------------------------------------------
			# Check for any Update Requests
			#
			# - This is where it can get very complicated, it might seem silly that we've pulled info if we are going to be
			# just replacing it right away, but not all needs replacing and we always need to compare it anyway, so if it does
			# replace any data we will just refresh their set variables and everything will run dandy.
			#------------------------------------------------------------------------------------------------------------------
		
			# Check for Update query
			if ($Queries[1] == '!')
			{
				// Work out last update difference
				$LastUpdateDifference = time() - $LastUdatedRaw;
	
				// Check for Maintenance
				if (MAINTENANCE)																										// 	If maintenance
					$UpdateResults = '<div style="padding:20px;"><div class="notice">'. PAD_NO_UPDATING_AGAIN .'</div></div>';
				else if (!$Session->Online)																								//	If not online
					$UpdateResults = '<div style="padding:20px;"><div class="notice">'. PAD_NO_UPDATING_OFFLINE .'</div></div>';
				else if ($LastUpdateDifference < $UpdateWait && !$Session->isAdmin())													//	If not enough time has passed
					$UpdateResults = '<div style="padding:20px;"><div class="notice">'. PAD_NO_UPDATING_SHORT .'</div></div>';
				else if ($Session->Character == $CICUID || ($LastUpdateDifference > 108000	&& $Update30Hour) || $Session->isAdmin())	//	If owner or time been 3 days
				{	
					$JustUpdated = true;
					include 'classes/pad_update.php';	
				}
				else
					$UpdateResults = '<div style="padding:20px;"><div class="warning">'. PAD_NO_UPDATING_UNKNOWN .'</div></div>';		//	Unknown
				
				// Load 	
				echo '<script>$(function() { $( "#pad" ).tabs({ selected: 7, collapsible: false }); });</script>';						//	Update Tab
			}
			
			# If just updated, get new achievements!!!!!!!
			if ($JustUpdated)
			{
				# Update Display
				unset($Achievements);
				unset($Fields);
				
				# Get Achievements
				$Fields = array(TBL_HISTORY_CICUID, 
								TBL_HISTORY_NMS, 
								TBL_HISTORY_FACTIONS, 
								TBL_HISTORY_CAMPS,
								TBL_HISTORY_QUESTS,
								TBL_HISTORY_GUILDSKILLS,
								TBL_HISTORY_MISC,
								TBL_HISTORY_GIL,
								TBL_HISTORY_ENEMIES_KILLED,
								TBL_HISTORY_LOCALLEVES_COMPLETE,
								TBL_HISTORY_REGIONLEVES_COMPLETE,
								TBL_HISTORY_TOTAL,
								TBL_HISTORY_COMPANY,
								TBL_HISTORY_DUNGEON,
								TBL_HISTORY_LEVELS,
								TBL_HISTORY_TALLIES,
								TBL_HISTORY_OTHER,
								TBL_HISTORY_PAGES,
								TBL_HISTORY_V3);
								
				$Achievements = $DB->GetData(TBL_HISTORY, $Fields, false, TBL_HISTORY_CICUID ." = '". $CICUID ."'");
				$Achievements = $Achievements[3];
				$Pages		  = $Achievements[TBL_HISTORY_PAGES];
				$Version	  = $Achievements[TBL_HISTORY_V3];
				
				# Set fields for stats/prev stats data and pull from Database.
				$Fields = array(TBL_STATS_PROFILE, TBL_STATS_ATTRIBUTES, TBL_STATS_RESISTANCES, TBL_STATS_LEVELS, TBL_STATS_EXP, TBL_STATS_COMPANY);		  
				$Stats = $DB->GetData(TBL_STATS, $Fields, false, TBL_STATS_CICUID ."='". $Character[TBL_CHARACTERS_CICUID] ."'");
				$Stats = $Stats[3];
				$Fields = array(TBL_STATS_AUTO, TBL_STATS_LEVELS, TBL_STATS_EXP);		  
				$PrevStats = $DB->GetData(TBL_STATS_HISTORY, $Fields, true, TBL_STATS_CICUID ."='". $Character[TBL_CHARACTERS_CICUID] ."'", TBL_STATS_AUTO, "DESC", array(0,10));
				//show($PrevStats);
				$HasPrev = $PrevStats[0];
				$PrevStats = $PrevStats[3];
				
				# Set some basic profile data
				$Profile_Data   = explode(":", $Stats[TBL_STATS_PROFILE]);
				$ActiveClass 	= str_replace("&nbsp;", "", $Profile_Data[0]);
				$Race 			= explode("|", $Profile_Data[1]);
				$Flag 			= str_replace(array(" ", "&#39;"), "", $Profile_Data[2]);
				$Guardian		= $Profile_Data[3];
				$Guardian_Icon	= explode(",", strtolower(str_replace("&#39;", "", $Guardian)));
				$Guardian_Icon  = $Guardian_Icon[0];
				$Birthdate		= str_replace("of the", "/", $Profile_Data[4]);
				$Birthdate		= str_replace("of the", "/", $Profile_Data[4]);
				$HP				= explode("-", $Profile_Data[7]);
				$MP				= explode("-", $Profile_Data[8]);
				$TP				= explode("-", $Profile_Data[9]);
				$Levels 		= explode(":", $Stats[TBL_STATS_LEVELS]);
				$Experience		= explode(":", $Stats[TBL_STATS_EXP]);
				$CompanyData	= explode("|", $Stats[TBL_STATS_COMPANY]);
				$GC_Rank		= $CompanyData[1];
				$GC_Icon		= $CompanyData[0];
				
				# Get index of Active Class then set Level and Experience Points.
				$ActiveClass_Index 	= array_search($ActiveClass, $Classes);
				$ActiveClass_Level 	= $Levels[$ActiveClass_Index];
				$ActiveClass_EXP 	= str_replace("|", " / ", $Experience[$ActiveClass_Index]);
				if ($ActiveClass_EXP == "0 / 0")
					$ActiveClass_EXP = "Capped";
				
				# Male/Female detection
				$FemaleDetection = stripos($Race[1], "Female");
				if ($FemaleDetection === false)
					$GenderIcon = "male_icon";
				else
					$GenderIcon = "female_icon";
				
				//Show($Achievements);
			}
			
			#------------------------------------------------------------------------------------------------------------------
			# Display Pad!
			#------------------------------------------------------------------------------------------------------------------
			?>
			<div class="pads">
				
				<!-- Avatar and "Be Friend" + "Send Message" buttons -->
				<div class="pleft">
				<div class="profile" id="profile">
					
					<!-- Avatar -->
					<div class="avatar with-tooltip2" title="<img src='<? echo $Mugshot; ?>' />" 
						 style="background-image:url('<? echo $Character[TBL_CHARACTERS_AVATAR]; ?>'); background-size:130px 130px; background-repeat:no-repeat; background-position:center;">
						<img src="images/borders/<? echo $Flag; ?>.png" name="AvatarFrame" width="150" height="150" />
					</div>
					
					
					<!-- Other Avatars -->				
					<?
					if (!empty($AvatarHistory) && !$SettingsPrivacy[3])
					{
						echo '<div align="center" style="margin-top:-5px;margin-bottom:5px;"><div class="otheravatars">';
						//show($AvatarHistory);
						foreach($AvatarHistory as $MiscAvatar)
							echo '<img src="'. $MiscAvatar .'" width="18" height="18" class="with-tooltip" title="<img src=\''. $MiscAvatar .'\' />" />';
							
						echo '</div></div>';
					}
					?>
					
					<!-- Buttons under Avatar -->
					<?
					if ($Session->Online && $Session->Character != $CICUID)
					{	
						# Add To Friends list
						// Check if query = like, online and
						$FriendArray = explode(",", $FL);
						if ($Queries[1] == 'like')
						{
							// Get Session Char
							$GetUserChar = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_NAME, false, TBL_CHARACTERS_CICUID ."='". $Session->Character ."'");
							// Make sure friend doesnt exit
							if (!in_array($CICUID, $FriendArray))
							{
								//show($FriendArray);
								// If no friends list was found, create a new one and add this user
								if (!$FList[0])
								{
									$DB->Insert(TBL_FRIENDS,
													  array(TBL_FRIENDS_CICUID 		."='". $Session->Character ."'",
															TBL_FRIENDS_ARRAY 		."='". $CICUID ."'"));
															
									$FriendArray = array($CICUID);
								}
								else
								{
									// if user has a friends list but it is empty, just add this ID
									if (empty($FL))
									{
										$string_array = $CICUID;
									}
									else
									{
										// Else we need to split it, append on new friend then implode it again
										$exitingarray = explode(",", $FL);
										$exitingarray[count($exitingarray)] = $CICUID;
										$string_array = implode(",", $exitingarray);
									}
									
									
									$DB->Update(TBL_FRIENDS,
													  array(TBL_FRIENDS_CICUID 		."='". $Session->Character ."'",
															TBL_FRIENDS_ARRAY 		."='". $string_array ."'"),
															TBL_FRIENDS_CICUID 		."='". $Session->Character ."'");	
															
									$FriendArray = explode(",", $string_array);			
									
								}
								
								$DB->Insert(TBL_MESSAGE,
												  array(TBL_MESSAGE_TO 			."='". $CICUID ."'",
														TBL_MESSAGE_FROM 		."='". $Session->Character ."'",
														TBL_MESSAGE_TIME 		."='". time() ."'",
														TBL_MESSAGE_IPLONG 		."='". ip2long($_SERVER['REMOTE_ADDR']) ."'",
														TBL_MESSAGE_SUBJECT 	."='New Friend: ". $GetUserChar[3][TBL_CHARACTERS_NAME] ."!'",
														TBL_MESSAGE_MESSAGE 	."='Dear ". $Name ."<br><strong>". $GetUserChar[3][TBL_CHARACTERS_NAME] ."</strong> has added you to their friends list! <br><br> (If you wish to remove them, click Manager > Friends)'",
														TBL_MESSAGE_STATUS 		."='1'",
														TBL_MESSAGE_TYPE 		."='friend'"));
								
								echo '<img src="images/icons/misc/tick.png" style="margin-bottom:-3px;"> <span style="color:#309300;font-size:11px;">'. FRIEND_ADDED .'</span>';
							}
						}
						//Show($FriendArray);
						if (!in_array($CICUID, $FriendArray))
						{
							?>
							<a href="?<? echo $CICUID; ?>&like"><img src="images/icons/comments/like.png" style="margin-bottom:-3px;"> <? echo PAD_BEFRIEND; ?></a>
							<?
						}
						
						?>
						<a href="?-PM&v=compose&n=<? echo $Name; ?>&i=<? echo $CICUID; ?>"><img src="images/icons/comments/comment.png" style="margin-bottom:-3px;"/> <? echo HEADER_SEND; ?></a>
						<?
					}
					else if ($Session->Online)
					{
						?>
						<a href="?-Tracker"><img src="images/large/titles/tracker.png" style="width:18px;height:18px;margin-bottom:-4px;"/> <? echo MENU_TRACKER; ?></a>   
						<?
					}
					?>
				
				</div>
				
				<!-- Profile Section -->
				<div class="subprofile">
				<h6>Profile</h6>
				<div>
				
				<!-- Company -->
				<?
				$GC_Color = NULL;
				if (stripos($GC_Rank, "Storm") !== false)
					$GC_Color = '#881515';
				else if (stripos($GC_Rank, "Serpent") !== false)
					$GC_Color = '#B76615';
				else if (stripos($GC_Rank, "Flame") !== false)
					$GC_Color = '#605353';
				else
					$GC_Color = '#666';
				?>
				<section>
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
				<td><img src="<? echo $GC_Icon; ?>" style="margin:-8px 4px -7px 0;" /></td>
				<td style="font-size:11px; font-weight:bold; color:<? echo $GC_Color; ?>;"><? echo $GC_Rank; ?></td>
				</tr>
				</table> 

				</section>
				
				<!-- Score -->
				<section><img src="images/icons/ffspecific/score.png" style="margin-bottom:-4px;" /> Score <strong style="color:#468C00;"><? echo $Score; ?></strong></section>
				
				<!-- Active Class -->
				<section  class="hover with-tooltip" 
						  title="<table width='130' border='0' cellspacing='0' cellpadding='2'>
								 <tr>
								 <td width='5%' align='center'><img src='<? echo GetClassIcon($ActiveClass); ?>' alt='' /></td>
								 <td width='95%'><strong>Lv. <? echo $ActiveClass_Level; ?></strong> <div style='font-size:11px;color:#444;'>(<? echo $ActiveClass_EXP; ?>)</div></td>
								 </tr>
								 </table>">
						  <img src="<? echo GetClassIcon($ActiveClass); ?>" style="margin-bottom:-4px;" width="18" height="18" /> 
						  Active <strong style="color:#000;"><? echo $ActiveClass; ?></strong>
				</section>
				
				<!-- Guardian -->
				<section><img src="images/large/twelve/<? echo $Guardian_Icon; ?>.png" style="margin-bottom:-4px;"  width="18" height="18" /> <? echo $Guardian; ?></section>
				
				<!-- Race -->
				<section  class="hover with-tooltip" 
						  title="<? echo $Race[1]; ?>">
						  <img src="images/icons/misc/<? echo $GenderIcon; ?>.png" style="margin-bottom:-4px;"  width="18" height="18" /> 
						  <strong><? echo $Race[0]; ?></strong>
				</section>
				
				<!-- Calender-->
				<section style="letter-spacing:-1px;"><img src="images/icons/settings/calender.png" style="margin-bottom:-4px; margin-right:3px; margin-left:2px;" /> <? echo $Birthdate; ?></section>
				
				<!-- Added -->
				<div class="added">
				<strong>Joined:</strong> <? echo $Added; ?>
				</div>
	
				
				</div>
				</div>
				</div>
				
				<div class="pright">
				
				<!-- Top Bar (Name/Server/Update/Flag) -->
				<div class="top_bar">
				
					<!-- Flag -->
					<img src="images/large/cities/<? echo $Flag; ?>.png" style="position:absolute; margin:-80px 0 0 660px; z-index:1; display:none;" class="padflag" />
				
					<!-- Name / Server -->
					<span class="name"><? echo $Name; ?></span>
					<span class="server">(<? echo $Server; ?>)</span>
					
					<div style="float:right; margin-right:130px; padding-top:5px;">
					
					<!-- Last Updated / Update Button -->
					<span class="lastupdated"><strong>Last Updated:</strong> <? echo $LastUdated; ?></span>
					<?
					if ($Session->Online)
					{
						//Show($LastUdatedRaw);
						$LastUpdateDifference = time() - $LastUdatedRaw;
						//Show($LastUpdateDifference);
						//Show($UpdateWait);
						if (MAINTENANCE)
						{
						?>
							<span class="noupdate with-tooltip" title="<? echo PAD_DISABLED_INFO; ?>">
							<? echo PAD_NOUPDATE; ?>
							<img src="images/icons/misc/cross2.png" style="vertical-align:bottom;"  name="updatebutton" />
							</span>
						<?
						}
						else if ($SettingsTimeline[1] && $Session->Character != $CICUID && !$Session->IsAdmin())
						{
							// Incase I want to show anything, which I do not.
						}
						//$Session->Character != $CICUID 
						else if ($SettingsTimeline[2] && (!in_array($Session->Character, $CharactersFriends) || !$Session->IsAdmin()) && $Session->Character != $CICUID)
						{
							// Incase I want to show anything, which I do not.
						}
						else if ($LastUpdateDifference > $UpdateWait)
						{
						?>
							<a class="update" href="?<? echo $CICUID; ?>&!" onclick="ChangeUpdateButton();"><span id="updatetext"><? echo PAD_UPDATE; ?></span>
							<img src="images/icons/misc/refresh.png" style="vertical-align:bottom;"  name="updatebutton" />
							</a>
						<?
						}
						else if ($Session->IsAdmin())
						{
						?>
							<a class="update" href="?<? echo $CICUID; ?>&!" onclick="ChangeUpdateButton();"><span id="updatetext" style="color:#F00;">Update</span>
							<img src="images/icons/misc/refresh.png" style="vertical-align:bottom;"  name="updatebutton" />
							</a>
						<?
						}
					}
					?>
					</div>
					
					<!-- Linkshells -->
					<?
						if ($Linkshells[0])
						{
							echo '<div class="linkshells">';
							foreach($Linkshells[3] as $LS)
							{
								$Icon = NULL;
								if ($CICUID == $LS[TBL_LINKSHELL_OWNER])
									$Icon = '<img src="images/icons/ffspecific/ls_master.png" width="16" height="16"/>';
								
								echo '<a href="?ls='. $LS[TBL_LINKSHELL_ID] .'" class="linkshell">
									  <img src="'. $LS[TBL_LINKSHELL_EMBLUM] .'" width="16" height="16"/>'. $Icon . $LS[TBL_LINKSHELL_NAME] .'</a> ';
							}
							echo '</div>';
						}
					?>
					
					
				</div>
				
				<!-- Loading Screen -->
				<div id="prepad" class="padloading"><? echo str_replace("%x", $Name, PAD_LOADING); ?></div>
				<div id="padloading_icon" style="width:100px;"></div>
				<script>
				/* Options for Spin.js */
				var opts = {
				  lines: 14, // The number of lines to draw
				  length: 1, // The length of each line
				  width: 3, // The line thickness
				  radius: 7, // The radius of the inner circle
				  color: '#555', // #rgb or #rrggbb
				  speed: 1.8, // Rounds per second
				  trail: 20, // Afterglow percentage
				  shadow: false // Whether to render a shadow
				};
				var target = document.getElementById('padloading_icon');
				var spinner = new Spinner(opts).spin(target);
				target.appendChild(spinner.el);
				</script>
				
				<!-- PADZ -->
				<div id="pad" class="tabs" style="z-index:2; display:none;">
						
					<!-- Tab Menu (Navigation) -->
					<ul class="menu" style="font-size:12px !important; font-family:Arial, Helvetica, sans-serif; border-bottom:solid 1px #8CAED1;">
						<li style="border-color:#97B4D2 !important;"><a href="#pad-1"><img src="images/icons/menu/profile.png" style="margin-bottom:-4px;" /> 		<? echo CHAROPTION_PROFILE; ?></a></li>
						<? if ($SettingsPrivacy[4]) { ?> <li style="border-color:#97B4D2 !important;"><a href="#pad-2"><img src="images/icons/menu/friends.png" style="margin-bottom:-4px;" /> <? echo CHAROPTION_FRIEND; ?></a></li> <? } ?>
						<? if ($SettingsPrivacy[5]) { ?> <li style="border-color:#97B4D2 !important;"><a href="#pad-3"><img src="images/icons/menu/spgains.png" style="margin-bottom:-4px;" /> <? echo CHAROPTION_SPGAINS; ?></a></li> <? } ?>
						<? if ($SettingsPrivacy[6]) { ?> <li style="border-color:#97B4D2 !important;"><a href="#pad-4"><img src="images/icons/menu/timeline.png" style="margin-bottom:-4px;" /> <? echo CHAROPTION_TIMELINE; ?></a></li> <? } ?>
						<? if ($SettingsPrivacy[7]) { ?> <li style="border-color:#97B4D2 !important;"><a href="#pad-5"><img src="images/icons/menu/classes.png" style="margin-bottom:-4px;" /> <? echo CHAROPTION_CLASSES; ?></a></li> <? } ?>
						<? if ($SettingsPrivacy[8]) { ?> <li style="border-color:#97B4D2 !important;"><a href="#pad-6"><img src="images/icons/menu/achievements.png" style="margin-bottom:-4px;" /> <? echo CHAROPTION_ACHIEVEMENTS; ?></a></li> <? } ?>
						<? if ($SettingsPrivacy[9]) { ?> <li style="border-color:#97B4D2 !important;"><a href="#pad-7"><img src="images/icons/menu/gear.png" style="margin-bottom:-4px;" /> <? echo CHAROPTION_GEARSETS; ?></a></li> <? } ?>
						
						<!-- Results Tab -->
						<li style="visibility:hidden;"><a href="#pad-8">Results</a></li>
					</ul>
					
					<!-- SPECIAL: Updated-->
					<div id="pad-8" class="frame" style="padding:20px;">
					<? echo $UpdateResults; ?>
					</div>
					
					<!------------------------------------------------------->
					
					<!-- Profile -->
					<div id="pad-1" class="frame">
					
						<!-- Ranking -->
						<div class="ranks">
						
							<!-- Total Events -->
							<div class="ranking rankingtop" style="border-bottom:solid 1px #bbb;">
							<strong>Events</strong>
							<span><? echo $Events; ?></span>
							</div>
						
							<!-- Ranking -->
							<div class="title">Ranking</div>
							<div class="inform"><strong>Server</strong> <br/> All / Total</div>
							
							<!-- Ranking: Events -->
							<div class="ranking">
							<strong>Events</strong>
							<?
								$rank_overall_db = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_AUTO, false, TBL_CHARACTERS_EVENTS ." > '". $Events ."'");
								$rank_server_db = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_AUTO, false, TBL_CHARACTERS_EVENTS ." > '". $Events ."' AND ". TBL_CHARACTERS_SERVER ." = '". $Server ."'");
								$rank_total_db = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_AUTO, false);
								
								$rank_overall = $rank_overall_db[0]+1;
								$rank_server = $rank_server_db[0]+1;
								$rank_total = $rank_total_db[0];
							?>
							
							<span> <? echo GetRankIcon($rank_server) . $rank_server; ?> </span>
							<br/>
							<? echo $rank_overall; ?> / <? echo $rank_total; ?>
							</div>
							
							<!-- Ranking: Classes -->
							<div class="ranking">
							<strong>Class Levels</strong>
							<?
								$rank_overall_db = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_AUTO, false, TBL_CHARACTERS_SCORE ." > '". $Score ."'");
								$rank_server_db = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_AUTO, false, TBL_CHARACTERS_SCORE ." > '". $Score ."' AND ". TBL_CHARACTERS_SERVER ." = '". $Server ."'");
								$rank_total_db = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_AUTO, false);
								
								$rank_overall = $rank_overall_db[0]+1;
								$rank_server = $rank_server_db[0]+1;
								$rank_total = $rank_total_db[0];
							?>
							
							<span> <? echo GetRankIcon($rank_server) . $rank_server; ?> </span>
							<br/>
							<? echo $rank_overall; ?> / <? echo $rank_total; ?>
							</div>
							
							<!-- Ranking: Achievements -->
							<div class="ranking">
							<strong>Achievements</strong>
							<?
								$rank_overall_db = $DB->GetData(TBL_HISTORY, TBL_HISTORY_AUTO, false, TBL_HISTORY_TOTAL ." > '". $Achievements[TBL_HISTORY_TOTAL] ."'");
								$rank_server_db = $DB->GetData(TBL_HISTORY, TBL_HISTORY_AUTO, false, TBL_HISTORY_TOTAL ." > '". $Achievements[TBL_HISTORY_TOTAL] ."' AND ". TBL_HISTORY_SERVER ." = '". $Server ."'");
								$rank_total_db = $DB->GetData(TBL_HISTORY, TBL_HISTORY_AUTO, false);
								
								$rank_overall = $rank_overall_db[0]+1;
								$rank_server = $rank_server_db[0]+1;
								$rank_total = $rank_total_db[0];
							?>
							
							<span> <? echo GetRankIcon($rank_server) . $rank_server; ?> </span>
							<br/>
							<? echo $rank_overall; ?> / <? echo $rank_total; ?>
							</div>
							
							<!-- Link to more -->
							<a href="?-Ranking">More Ranking</a>
							
						</div>
					
						<!-- Media and Events -->
						<div class="details">
						<div style="padding:10px;">
	   
							<!-- List Events -->
							<h6>Recent Activity</h6>
							<div class="events">
								<?
								foreach($RecentEvents as $Event)
									echo '<div class="event with-tooltip" title="'. str_replace("\"", "'", $Event[TBL_EVENTS_DETAILS]) .'">'. iconfix(Learn2Spell(Translate($Event[TBL_EVENTS_SUBJECT]))) .'</div>';
								?>
							</div>
						
							<br/><br/>
						
							<!-- Display some Media -->
							<h6>Images and Videos</h6>
							<div class="media">
							
								<!-- Images -->
								<div id="v-images">
								<?
								$mediafolder = "files/char". $CICUID ."/mugshots/";
								if (is_dir($mediafolder))
								{
									$Directory_Thumbs = $mediafolder .'/thumb/';
									$Directory_Scaled = $mediafolder .'/small/';
									$Directory_Original = $mediafolder .'/original/';
									$mediahandle = opendir($Directory_Thumbs);
									
									// Making an array containing the files in the current directory:
									while ($mfile = readdir($mediahandle)) 
										$mediafiles[] = $mfile;
										
									// Close Directory
									closedir($mediahandle);
									
									// Remove directory actions
									$mediafiles = array_values(array_diff($mediafiles,array('.')));
									$mediafiles = array_values(array_diff($mediafiles,array('..')));
									$mediafiles = array_reverse($mediafiles);
		
									$i = 0;
									$ImageLimit = 6;
									foreach($mediafiles as $mfile)
									{
										// Get Image Sizes
										$size = getimagesize($Directory_Thumbs . $mfile);
										
										if ($size[0] > 230)
											continue;
										else if ($size[0] > 110)
											$ImageLimit--;
										
										
											
										echo '<img src="'. $Directory_Thumbs . $mfile .'" id="img-'. $i .'" style="display:none;" class="with-tooltip2" title="<img src=\''. $Directory_Scaled . $mfile .'\' style=\'box-shadow: 0 0 3px #999;\' />" />';	
										$i++;
										
										if ($i == $ImageLimit)
											break;
									}
								}
								else
									echo '<div style="padding:4px;font-size:10px;color:#999;">No Images!</div>';
								?>
								</div>
								
								<!-- Youtube -->
								<div id="v-utube" style="display:none;">
								<?
								$VideoFile = "files/char". $CICUID ."/videos/pad";
								if (file_exists($VideoFile))
								{
									// Get Video links
									$VideoLinks = explode("\n", file_get_contents($VideoFile));
									$VideoLinks = array_reverse($VideoLinks);
	
									// Show videos
									$i = 0;
									$VideoLimit = 6;
									foreach($VideoLinks as $Video)
									{
										if (empty($Video))
											continue;
											
										$videourl = str_replace("watch?v=", "embed/", $Video);
										$ID = str_replace("http://www.youtube.com/embed/", "", $videourl);
										
										echo '<a href="'. $Video .'" target="_blank"><img src="http://img.youtube.com/vi/'. $ID .'/2.jpg" width="99" height="62" class="with-tooltip2"
												Title="<img src=\'http://img.youtube.com/vi/'. $ID .'/0.jpg\'" /></a>';
											
										$i++;
										
										if ($i == $VideoLimit)
											break;
										
									}
								}
								else
									echo '<div style="padding:4px;font-size:10px;color:#999;">No Youtube Videos</div>';
								?>
								</div>
								
							</div>
							
							<!-- Media Views -->
							<div class="views">
								<?
								if (count($mediafiles) > 0)
								 echo '<a href="#" id="viewbutton1" onclick="SwitchMediaView(\'images\');"><img src="images/icons/misc/image.png" style="margin-bottom:-3px;" /></a>
									<span id="split"></span>';
								else if (count($VideoLinks) > 0)
								{
									echo '<script>SwitchMediaView(\'utube\');</script>';
								}
									
								if (count($VideoLinks) > 0)
									echo '<a href="#" id="viewbutton2" onclick="SwitchMediaView(\'utube\');"><img src="images/icons/misc/utube.png" style="margin-bottom:-3px;" /></a>';
								?>
								
								
							</div>
							
												
						</div>
						</div>
					
						<img src="images/large/fade.png" style="float:left;" />
					
						<!-- Chatbox -->
						<div class="chat">
						
							<section class="chatcontents" id="chatrefresh2">
								<span style="font-size:15px; color:#E69B2B; text-align:center; display:block; margin-top:50px;">
								<img src="images/large/misc/chocowalk.gif" style="vertical-align:bottom" />
								Waiting for pad to load, ~kweh!</span>
							</section>
						
							<script type="text/javascript"> 
							<? if (!$Editing)
							{
								?>	
								$(function() 
								{ 
									setPadID("<? echo $CICUID; ?>");
									startActivityRefresh_3(); 
								}); 
								<?
							}
							?>
							DisplayPadChat("<? echo $CICUID; ?>");
							</script>
							
						</div>
						
						<?
						if ($Session->Online && !empty($Session->Character))
						{
						?>
							<div class="chatinput form">
								<input name="chatmessage" type="text" id="chatmessage" style="width:320px;" onkeyup='checkKey3(event);' value="" maxlength="500" />
								<button onclick='send_pad_message()'><strong><? echo CHAT_POST; ?></strong></button>
								<a href="?-Padlog&i=<? echo $CICUID; ?>"><? echo CHAT_LOG; ?></a>
								<div id="display"></div>
							</div>
						<?
						}
						else if ($Session->Online && empty($Session->Character))
							echo '<div class="intro notlogged">You must have a verified character to chat.</div>';
						else
							echo '<div class="intro notlogged">Login to chat!</div>';
						?>
						
						
					<br style="clear:both;" />
					</div>
					
					<!-- Friends -->
					<? 
					if ($SettingsPrivacy[4])
					{
						echo '<div id="pad-2" class="frame">';
						include "classes/pad_friends.php"; 
						echo '</div>';
					}
					?>
					
					
					<!-- SP Gains -->
					<? 
					if ($SettingsPrivacy[5])
					{
						echo '<div id="pad-3" class="frame">';
						include "classes/pad_spgains.php"; 
						echo '</div>';
					}
					?>
					
					
					<!-- Timeline -->	
					<? 
					if ($SettingsPrivacy[6])
					{
						echo '<div id="pad-4" class="frame">';
						include "classes/pad_timeline.php"; 
						echo '</div>';
					}
					?>
				
					<!-- Classes -->
					<? 
					if ($SettingsPrivacy[7])
					{
						echo '<div id="pad-5" class="frame">';
						include "classes/pad_classes.php"; 
						echo '</div>';
					}
					?>
					
					<!-- Achievements -->
					<? 
					if ($SettingsPrivacy[8])
					{
						echo '<div id="pad-6" class="frame">';
						include "classes/pad_achievements.php";
						echo '</div>'; 
					}
					?>
					
					<!-- Gear -->					
					<? 
					if ($SettingsPrivacy[9])
					{
						echo '<div id="pad-7" class="frame">';
						include "classes/pad_gear.php";
						echo '</div>';
					}
					?>
						
				</div>
				</div>
				
			</div>
			<?
		}
	}
	else
		echo '<div class="error">'. CHARACTER_NOT_FOUND .'</div>';
?>
<style>
@import "css/flick/end.css";
</style>